<template>
  <div class="edu-page-warp reply-arrange">
    <!--查看老师/学生详情弹框-->
    <TeacherStuInfoDialog :viewDetailData="viewDetailData" :dialogVisible="dialogVisible" :viewType="viewType" @taacher-stu-close="closeDialog"></TeacherStuInfoDialog>

    <condition-card @reset="reset" @search="searchList" label-width="110px">
      <el-row>
        <el-col :span="8">
          <el-form-item :label="$t('graduationDesign.graduationYear')">
            <el-date-picker class="search-item" :clearable="false" v-model="queryForm.graduationYear" @change="handleGradeChange" value-format="yyyy" type="year" :placeholder="$t('graduationDesign.pleaseChoose')">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item :label="$t('graduationDesign.adminCollege')">
            <el-select v-model="queryForm.collegeId" filterable @change="handleCollegeChange" class="search-item" clearable>
              <el-option v-for="item in source.studentColleges" :key="item.collegeId" :label="item.collegeIdI18n" :value="item.collegeId" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item :label="$t('graduationDesign.major')">
            <el-select v-model="queryForm.subjectId" filterable class="search-item" clearable>
              <el-option v-for="item in source.subjectOptions" :key="item.subjectId" :label="item.subjectName" :value="item.subjectId" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item :label="$t('graduationDesign.replyGroup')">
            <el-input v-model="queryForm.defenseGroupName" :placeholder="$t('graduationDesign.pleaseEnter')" class="search-item" clearable></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </condition-card>
    <el-card>
      <div class="edu-page-title">{{$t('graduationDesign.studentList')}}</div>
      <div class="edu-opertion-bar">
        <div class="float-left">
          <el-input v-model="queryForm.studentName" :placeholder="$t('graduationDesign.pleaseEnterStuSearch')" style="width:300px" @keyup.enter.native="searchList" clearable>
            <i slot="suffix" class="el-icon-search" style="margin-top:10px;cursor:pointer" @click="searchList"></i>
          </el-input>
        </div>
        <div class="float-right">
          <el-button @click="onExport" v-loading.fullscreen.lock="loading" element-loading-text="Loading" element-loading-spinner="el-icon-loading" element-loading-background="rgba(255, 255, 255, .48)">{{$t('graduationDesign.export')}}</el-button>
        </div>
      </div>

      <edu-table ref="myStudentTable" :url="getList" @selection-change="handleSelectChange">
        <edu-table-column :label="$t('graduationDesign.graduationYear')" prop="graduationYear" min-width="80"></edu-table-column>
        <edu-table-column :label="$t('graduationDesign.replyStudent')" prop="studentName" min-width="100"></edu-table-column>
        <edu-table-column :label="$t('graduationDesign.replyLocation')" prop="defenseLocation" min-width="100"></edu-table-column>
        <edu-table-column :label="$t('graduationDesign.replyTime')" prop="defenseStartTime" min-width="110"></edu-table-column>
        <edu-table-column :label="$t('graduationDesign.replyGroup')" prop="defenseGroupName" min-width="100"></edu-table-column>
        <edu-table-column :label="$t('graduationDesign.result')" prop="defenseResult" min-width="90">
          <template slot-scope="scope">
            <span>{{defenseResultFormat(scope.row.defenseResult)}}</span>
          </template>
        </edu-table-column>
      </edu-table>
    </el-card>
  </div>
</template>
<script>
import API from "common/src/api/underGraduationDesign/assignReplyStudent";
import tools from 'common/src/views/underGraduationDesign/tools.js';
export default {
  name: 'myStudent',
  data() {
    return {
      api: API,
      getList: '/api/designservice/defenseGroup/queryMyDefenseStudent',
      queryForm: {
        graduationYear: '',
        collegeId: '',
        subjectId: '',
        defenseGroupName: '',
        studentName: ''
      },
      loading: false,
      dialogVisible: false,
      source: {
        studentColleges: [], // 行政学院
        collegeOptions: [], // 学院
        subjectOptions: [], // 专业
        // 成绩
        resultTypeOptions: [{
          label: '优',
          value: 0
        }, {
          label: '良',
          value: 1
        }, {
          label: '中',
          value: 2
        }, {
          label: '及格',
          value: 3
        }, {
          label: '不及格',
          value: 4
        }, {
          label: '无效',
          value: 5
        }]
      },
      selectedData: [], // 勾选的列表数据
      viewDetailData: []
    };
  },
  mounted() {
    this.queryForm.graduationYear = new Date().getFullYear() + '';
    this.getCollege();
    this.searchList();
    tools.initAllCollege(this.source.studentColleges);
  },
  methods: {
    searchList() {
      this.$refs.myStudentTable.search(this.queryForm);
    },
    reset() {
      this.queryForm = {
        graduationYear: '',
        collegeId: '',
        subjectId: '',
        defenseGroupName: '',
        studentName: ''
      };
      this.source.subjectOptions = [];
      this.queryForm.graduationYear = new Date().getFullYear() + '';
      this.searchList();
    },
    handleSelectChange(val) {
      this.selectedData = val;
    },
    // 查学院
    getCollege() {
      this.api.getCollege().then(res => {
        this.source.collegeOptions = res;
      });
    },
    handleGradeChange(val) {
      this.queryForm.subjectId = "";
      this.source.subjectOptions = [];
      if (val && this.queryForm.collegeId) {
        this.handleCollegeChange(this.queryForm.collegeId);
      }
    },
    // 根据毕业年份和学院查专业
    handleCollegeChange(val) {
      this.queryForm.subjectId = "";
      this.source.subjectOptions = [];
      if (val && this.queryForm.graduationYear) {
        const params = {
          grade: this.queryForm.graduationYear,
          college: val
        };
        this.api.getSubject(params).then(res => {
          this.source.subjectOptions = res.data;
        }).catch();
      }
    },
    // 成绩过滤显示
    defenseResultFormat(val) {
      let resultItem = this.source.resultTypeOptions.find(item => item.value === val);
      return resultItem ? resultItem.label : "";
    },
    // 学生信息详情
    onDetail(row, type) {
      this.viewType = type;
      let roleType = type === 'TEACHER' ? '0' : '1';
      this.api.getTeacherStuInfo(row.id, roleType).then(res => {
        this.viewDetailData = res.data;
        this.dialogVisible = true;
      }).catch();
    },
    closeDialog() {
      this.dialogVisible = false;
    },
    // 导出
    onExport() {
      const params = {
        condition: this.queryForm,
        selectList: this.selectedData
      };
      this.api.exportMyStu(params).then(res => {
        if (res.code == 200) {
          // this.delayRequest(res.data.key);
          tools.downloadFile(res.data.key, "答辩安排及成绩维护(导师).xls", null, this);
        }
      });
    },
    // 查文件导出状态，等待导出
    async delayRequest(key) {
      this.loading = true;
      await new Promise(resolve => setTimeout(resolve, 3000));
      this.api.getExportStatus(key).then(res => {
        this.loading = false;
        if (res.code === 200 && res.data.status) {
          let path = 'api/designservice/common/export?path=' + res.data.path;
          window.open(path);
        } else {
          this.delayRequest(key);
        }
      }).catch(err => {
        this.loading = false;
        this.delayRequest(key);
      });
    }
  },
  components: {
    TeacherStuInfoDialog: () => import('../../assignReplyStudent/components/teacherStuInfoDialog')
  }
}
</script>
<style lang="scss" scoped>
.reply-arrange {
    .search-item {
        width: 100%;
    }
}
</style>
